﻿using DataSync.Entity;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using MySql.Data.MySqlClient;

namespace DataSync.Common
{
    class DBFactory
    {
        public static IDbConnection GetConn(DBConfig conf)
        {
            switch (conf.DBType)
            {
                case DataBaseType.MySql: return new MySqlConnection(conf.ConnString);
                case DataBaseType.Oracle: return new OracleConnection(conf.ConnString);
                case DataBaseType.SqlServer: return new SqlConnection(conf.ConnString);
                default:
                    return new OleDbConnection(conf.ConnString);
            }
        }

        public static IDbDataAdapter GetDataAdapter(DataBaseType dbType)
        {
            switch (dbType)
            {
                case DataBaseType.MySql: return new MySqlDataAdapter();
                case DataBaseType.Oracle: return new OracleDataAdapter();
                case DataBaseType.SqlServer: return new SqlDataAdapter();
                default:
                    return new OleDbDataAdapter();
            }
        }

        public static DbType GetDbType(DBConfig conf, DataColumn dc)
        {
            switch (conf.DBType)
            {
                case DataBaseType.Oracle:
                    switch (dc.DataType.Name)
                    {
                        case "Date": return DbType.Date;
                        case "DateTime": return DbType.Date;  //日期类型
                        case "Decimal": return DbType.Decimal;
                        case "Double": return DbType.Double;
                        case "Int16": return DbType.Int16;
                        case "Int32": return DbType.Int32;
                        case "Int64": return DbType.Int64;
                        case "Single": return DbType.Single;
                        default:
                            return DbType.String;
                    }
                default:
                    switch (dc.DataType.Name)
                    {
                        case "Date": return DbType.DateTime;
                        case "DateTime": return DbType.DateTime;
                        case "Decimal": return DbType.Decimal;
                        case "Double": return DbType.Double;
                        case "Int16": return DbType.Int16;
                        case "Int32": return DbType.Int32;
                        case "Int64": return DbType.Int64;
                        case "Single": return DbType.Single;
                        default:
                            return DbType.String;
                    }
            }

        }

        public static DbType GetDbType(string fieldType)
        {
            switch (fieldType.ToUpper())
            {
                case "DATE": return DbType.Date;
                case "DATETIME": return DbType.Date;  //日期类型
                case "DECIMAL": return DbType.Decimal;
                case "DOUBLE": return DbType.Double;
                case "INT16": return DbType.Int16;
                case "INT":
                case "INT32": return DbType.Int32;
                case "LONG":
                case "INT64": return DbType.Int64;
                case "FLOAT":
                case "SINGLE": return DbType.Single;
                default:
                    return DbType.String;
            }
        }

        public static Type GetSysType(string fieldType)
        {
            switch (fieldType.ToUpper())
            {
                case "DATE": return typeof(DateTime);
                case "DATETIME": return typeof(DateTime);
                case "DECIMAL": return typeof(decimal);
                case "DOUBLE": return typeof(double);
                case "INT16": return typeof(Int16);
                case "INT":
                case "INT32": return typeof(int);
                case "LONG":
                case "INT64": return typeof(long);
                case "FLOAT":
                case "SINGLE": return typeof(float);
                default:
                    return typeof(string);
            }
        }

        public static string FormatSql_Count(DataBaseType dbType)
        {
            return "select count(*) from ({0}) tb ";
        }
        public static string FormatSql_Page(DataBaseType dbType)
        {
            string pageSql = string.Empty;
            switch (dbType)
            {
                case DataBaseType.SqlServer:
                    pageSql = @"select * from (
 select row_number() over (order by __tc__) __rn__,* from (
   select top ({0}+{1}) 0 as __tc__,* FROM  ( 
{2}
  )x	)t ) tt where __rn__ > {0} ";
                    break;
                case DataBaseType.MySql:
                    pageSql = @"{2}  limit {0},{1} ";
                    break;
                case DataBaseType.PostgreSQL:
                    pageSql = @"{2}  limit {0},{1} ";
                    break;
                case DataBaseType.Oracle:
                    pageSql = @"select * from (select rownum as rw,a1.* from( 
 {2}
 )a1 ) m where rw <=({0} + {1}) and rw> {0} ";
                    break;
                case DataBaseType.Informix:
                    pageSql = @"select skip {0} first {1} from( {2} ) ";
                    break;
                default:
                    break;
            }
            return pageSql;
        }

    }
}
